package recycle.handler;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.ext.web.RoutingContext;
import recycle.common.Result;
import recycle.utils.AesUtils;

public class LoginInterceptor implements Handler<RoutingContext> {
    @Override
    public void handle(RoutingContext ctx) {
        // 排除登录和注册接口
        String path = ctx.request().path();
        if (path.startsWith("/users/login") || path.startsWith("/users/register") || path.startsWith("/users/exist")) {
            ctx.next();
            return;
        }

        // 检查认证令牌
        String token = ctx.request().getHeader("Authorization");
        if (token == null || !validateToken(token)) {
            ctx.json(Result.error("Unauthorized"));
            return;
        }

        String userId = AesUtils.decrypt(token);
        Vertx.currentContext().put("userId", userId);
        // 放行
        ctx.next();
    }

    private boolean validateToken(String token) {
        return true;
    }
}